package com.huanying.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.huanying.framework.BaseDao;
import com.huanying.framework.parameter.ParameterService;


public class Get58 {
	
	private static Logger logger = Logger.getLogger(Get58.class);
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private ParameterService parameterService;

	public void get() throws Exception {
		for (int j = 1; j <= 1; j++) {
			Document doc = Jsoup.connect("http://lf.58.com/lfguangyang/ershoufang/0/pn" + j + "/").get();
			Elements link_list = doc.getElementsByAttributeValue("class", "bthead");
			Element link;
			String linkUrl;
			for (int i = 0; i < link_list.size(); i++) {
				link = link_list.get(i).getElementsByTag("a").first();
				linkUrl = link.attr("href");
				System.out.println(linkUrl);
			}
		}
	}
	
	/**
	 * @param url
	 * @throws Exception
	 * @author Alan Yu
	 * @date 2017年4月14日
	 */
	public void handle(String url) throws Exception{
		//parameterService.intParamObject();
		//define variable
		Document doc;
		Sale building = new Sale();
		String temp_str;
		Elements temp_eles;
		Element temp_ele;
		//get html by url
		doc = Jsoup.connect(url).get();
		
		//get data
		String name = doc.getElementsByTag("h1").first().html().trim();
		System.out.println(name);
//		building.setName(_element.html().trim());		//房屋名称
		
		temp_ele = doc.getElementsByAttributeValue("class", "general-item-left").get(1);
		String home_type = temp_ele.getElementsByAttributeValue("class","c_000").get(1).html().trim();
		System.out.println(home_type);
//		building.setHome_type(home_type);				//用途
		
		String fitment_type = doc.getElementsByAttributeValue("class", "sub").get(1).html().trim();
		System.out.println(fitment_type);
//		building.setFitment_type(fitment_type);				//装修标准（参数表）	取值见Parameter.java注释
		
		String build_year = doc.getElementsByAttributeValue("class", "sub").get(2).html().trim();
		if("建筑年代(暂无信息)".equals(build_year)){
			build_year = "";
		}
		System.out.println(build_year);
//		building.setBuild_year(build_year);					//建筑年代
		
		String toward = doc.getElementsByAttributeValue("class", "main").get(2).html().trim();
		if("朝向(暂无信息)".equals(toward)){
			toward="";
		}
		System.out.println(toward);
//		building.setToward(toward);				//朝向
		
		String total_price = doc.getElementsByAttributeValue("class", "price").first().html().trim();
		total_price = total_price.substring(0, total_price.indexOf("<b>"));
		System.out.println(total_price);
//		building.setTotal_price(total_price);				//总价
		
		String unit_price = doc.getElementsByAttributeValue("class", "unit").first().html().trim();
		unit_price = unit_price.substring(0, unit_price.indexOf("元"));
		System.out.println(unit_price);
//		building.setUnit_price(unit_price);					//单价
		
		String build_space = doc.getElementsByAttributeValue("class", "main").get(1).html().trim();
		build_space = build_space.substring(0,build_space.indexOf("平"));
		System.out.println(build_space);
//		building.setBuild_space(build_space);				//建筑面积
		
		temp_str = doc.getElementsByAttributeValue("class", "main").first().html().trim();
		String bedroom_num = temp_str.substring(0, temp_str.indexOf("室"));
		String livingroom_num = temp_str.substring(temp_str.indexOf("室")+1, temp_str.indexOf("厅"));
		String toilet_num = temp_str.substring(temp_str.indexOf("厅")+1, temp_str.indexOf("卫"));
		System.out.println(bedroom_num);
//		building.setBedroom_num(bedroom_num);				//卧室数
		System.out.println(livingroom_num);
//		building.setLivingroom_num(livingroom_num);			//厅数
		System.out.println(toilet_num);
//		building.setToilet_num(toilet_num);					//卫生间数
		
		temp_str=doc.getElementsByAttributeValue("class", "sub").first().html().trim();
		if(!temp_str.equals("楼层(暂无信息)")){
			String floor_num = temp_str.substring(0,temp_str.indexOf("/"));
			String totle_floor = temp_str.substring(temp_str.indexOf("共")+1,temp_str.length()-1);
			System.out.println(floor_num);
			System.out.println(totle_floor);
		}
//		building.setFloor_num(floor_num);					//楼层
//		building.setTotle_floor(totle_floor);				//总层数
		
		String community_str = doc.getElementsByAttributeValue("class", "c_000 mr_10").first().html().trim();
		if("暂无信息".equals(community_str)){
			long community_id = 0;
		}
		else{
			System.out.println(community_str);
		}
//		building.setCommunity_id(community_id);				//小区表外键
		
		temp_eles = doc.getElementsByAttributeValue("class", "general-item-right");
		String ownership_years_str;
		if(temp_eles != null){
			temp_eles = temp_eles.first().getElementsByTag("li");
			for(int i=0; i<temp_eles.size(); i++){
				if(temp_eles.get(i).html().indexOf("产权年限")>0){
					ownership_years_str=temp_eles.get(i).getElementsByAttributeValue("class", "c_000").first().html();
					ownership_years_str = ownership_years_str.substring(0,ownership_years_str.indexOf("年"));
					System.out.println(Integer.valueOf(ownership_years_str));
					break;
				}
			}
		}
//		uilding.setOwnership_years(ownership_years);		//产权年限
		
		temp_ele = doc.getElementsByAttributeValue("class", "general-item-left").get(1);
		String ownership_type_str;
		if(temp_ele != null){
			temp_eles = temp_ele.getElementsByTag("li");
			for(int i=0; i<temp_eles.size(); i++){
				if(temp_eles.get(i).html().indexOf("交易权属")>0){
					ownership_type_str=temp_eles.get(i).getElementsByAttributeValue("class", "c_000").first().html();
					System.out.println(ownership_type_str);
					break;
				}
			}
		}
//		building.setOwnership_type(ownership_type);			//权属（参数表）	取值见Parameter.java注释
		
		building.setCreat_time(new Timestamp(System.currentTimeMillis()));					//发布时间
		
		
//		building.setHome_type(home_type);					//建筑类型（参数表）	取值见Parameter.java注释
//		building.setBuild_structure(build_structure);		//建筑结构（参数表）	取值见Parameter.java注释
//		building.setRefresh_time(refresh_time);				//刷新时间
//		building.setBuilding_num(building_num);				//栋、座
//		building.setUnit_num(unit_num);						//单元号
//		building.setHouse_num(house_num);					//门号
//		building.setBalcony_num(balcony_num);				//阳台数
//		building.setInside_space(inside_space);				//套内面积
//		building.setFloor_price(floor_price);				//底价
//		building.setLift_num(lift_num);						//梯数
//		building.setHome_num(home_num);						//户数
//		building.setCredentials_createdate(credentials_createdate);		//出征日期
//		building.setCredentials_num(credentials_num);		//权证编号
//		building.setCheck_num(check_num);					//(房管局)核验编号
//		building.setIs_frontage(is_frontage);				//是否临街
//		building.setLeave_date(leave_date);					//腾房时间
		
		
		//get images
		temp_eles = doc.getElementsByAttributeValue("class", "general-pic-list").first().getElementsByTag("li");
		for(int i=0; i<temp_eles.size(); i++){
			String img_src = temp_eles.get(i).getElementsByTag("img").first().attr("data-src");
			long t = System.currentTimeMillis();//获得当前时间的毫秒数
			System.out.println(img_src);
			//save_img(img_src,String.valueOf(t)+".jpg");
		}
		
	}
	
	public void save_img(String img_src,String fileName) throws Exception{
		URL url = new URL(img_src);
		URLConnection uc = url.openConnection();
		InputStream is = uc.getInputStream();
		File file = new File(fileName);
		FileOutputStream out = new FileOutputStream(file);
		int i = 0;
		while ((i = is.read()) != -1) {
			out.write(i);
		}
		is.close();
	}
	
	public static void main(String[] args) {
		Get58 get = new Get58();
		try {
			get.handle("http://lf.58.com/ershoufang/30655499228230x.shtml?from=1-list-7&iuType=p_0&PGTID=0d30000c-0030-4f48-4f5f-9ad98c7f7142&ClickID=1");
			//get.get();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
